package com.dating.activity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.dating.entities.AsyntaskDating;
import com.dating.libs.client.parttern.doit.OnTaskListener;
import com.dating.libs.web.services.datingservice.DatingService;
import com.dating.pref.Function;

public class ShowImage extends Activity {
	LinearLayout lyContent;
	ImageView imImage;
	TextView headTitle;
	ProgressBar headLoad;
	ArrayList<Bitmap> listImage = new ArrayList<Bitmap>();
	ArrayList<String> listUrl = new ArrayList<String>();
	ArrayList<ImageView> listImageView = new ArrayList<ImageView>();

	int userID = 82;

	Handler mHandler;
	OnTaskListener loadImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.show_image);

		lyContent = (LinearLayout) findViewById(R.id.ly_content);
		imImage = (ImageView) findViewById(R.id.im_image);

		headTitle = (TextView) findViewById(R.id.head_title);
		headLoad = (ProgressBar) findViewById(R.id.head_progressbar);

		mHandler = new Handler();
		initListener();

		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null && bundle.containsKey("user_id")) {
			try{
			// get avatar
			userID = Integer.valueOf(bundle.getString("user_id"));
			listUrl = getListURL(userID);
			loadImage(listUrl);
			}catch(NumberFormatException e){}
		} else {
			// default
			// default
			Toast.makeText(this, "can not get id of user", Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	private ArrayList<String> getListURL(int userID) {
		DatingService dt = new DatingService();
		try {
			listUrl = dt.getUserPhoto(83, 10);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listUrl;

	}

	private void initListener() {
		loadImage = new OnTaskListener() {

			@Override
			public void onPrepare(String... params) {
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						headLoad.setVisibility(View.VISIBLE);
					}
				});
				onDoBackground();
			}

			Bitmap mbmp;

			@Override
			public void onDoBackground(String... params) {
				ArrayList<String> mListUrl = listUrl;
				for (String url : mListUrl) {
					try {
						mbmp = Function.getBitmapFromUrl(new URL(url));
						mHandler.post(new Runnable() {

							@Override
							public void run() {
								addImage(mbmp.copy(Config.ALPHA_8, true));
							}
						});
					} catch (MalformedURLException e) {
						e.printStackTrace();
						continue;
					}
				}
			}

			@Override
			public void onComplete(Boolean values) {
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						headLoad.setVisibility(View.INVISIBLE);
					}
				});
			}
		};
	}

	private void loadImage(ArrayList<String> listUrl) {
		AsyntaskDating mAsyn = new AsyntaskDating();
		mAsyn.setOnTaskListener(loadImage);
		mAsyn.execute();
	}

	public void addImage(Bitmap bmp) {
		listImage.add(bmp);
		ImageView imv = new ImageView(this);
		imv.setLayoutParams(new ViewGroup.LayoutParams(
				WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.MATCH_PARENT));
		imv.setTag(bmp);
		listImageView.add(imv);
		imImage.setImageBitmap(bmp);
		lyContent.addView(imv);

		imv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				imImage.setImageBitmap((Bitmap)v.getTag());
			}
		});
	}
}
